const HandlerManager = require("../containers/HandlerManager")
const HandlerAdaptorManager = require("../containers/HandlerAdaptorManager")

module.exports = class DispatcherRoute {
  static getInstance() {
    return new DispatcherRoute()
  }

  init(baseDir) {
    this.handlerManager = new HandlerManager(baseDir)
    this.handlerAdaptorManager = new HandlerAdaptorManager()
  }

  handleRequest(req, res){
    this.doDispatch(req, res)
  }

  doDispatch(req, res) {
    debugger
    try {
      const mappedRoute = this.getHandler(req)
      //const ha = this.getHandlerAdaptor(mappedHandler)
      //const mv = ha.handler(req, res, mappedHandler)
      mappedRoute.handler(req, res)
    } catch (e) {
      console.log(e.toString())
    } finally {
      res.end()
    }
  }

  getHandler(req) {
    return this.handlerManager.getHandler(req)
  }

  getHandlerAdaptor(handler) {

  }

  noHandlerFound(req, res) {

  }
}
